package com.feri.buy.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feri.buy.auth.dao.UserDeviceMapper;
import com.feri.buy.auth.dao.UserLogMapper;
import com.feri.buy.auth.dao.UserMapper;
import com.feri.buy.auth.entity.User;
import com.feri.buy.auth.entity.UserDevice;
import com.feri.buy.auth.entity.UserLog;
import com.feri.buy.auth.service.intf.UserService;
import com.feri.buy.common.bo.UserAddBo;
import com.feri.buy.common.bo.UserFindBo;
import com.feri.buy.common.config.RabbitMQConfig;
import com.feri.buy.common.config.SystemConfig;
import com.feri.buy.common.util.EncryptUtil;
import com.feri.buy.common.util.StringUtils;
import com.feri.buy.common.vo.R;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper mapper;
    @Autowired
    private UserLogMapper logMapper;
    @Autowired
    private UserDeviceMapper deviceMapper;
    @Autowired
    private RabbitTemplate template;
    @Override
    public R check(String phone) {
        //验证参数
         if(StringUtils.isNotEmpty(phone)){
             //构建查询条件对象
             QueryWrapper<User> queryWrapper=new QueryWrapper<>();
             queryWrapper.eq("phone",phone);
             //获取查询结果
             User user=mapper.selectOne(queryWrapper);
             //验证
             if(user==null){
                 return R.ok();
             }else {
                 if(user.getFlag()== SystemConfig.FLAG_ERROR){
                     return R.fail("亲，你的账号已无效！");
                 }else {
                     return R.fail("亲，账号已存在！");
                 }
             }
         }
        return R.fail("亲，参数异常！");
    }

    @Override
    @Transactional //事务
    public R register(UserAddBo bo) {
        //校验
        if(bo!=null){
            QueryWrapper<User> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("phone",bo.getPhone());
            //获取查询结果
            User user=mapper.selectOne(queryWrapper);
            //验证是否可以注册
            if(user==null){
                //新增用户信息
                user=new User();
                user.setFlag(SystemConfig.FLAG_OK);
                user.setPhone(bo.getPhone());
                //密码-密文
                user.setPassword(EncryptUtil.aesenc(SystemConfig.PASS_KEY,bo.getPass()));
                mapper.insert(user);
                //新增用户设备信息
                deviceMapper.insert(new UserDevice(bo.getBrand(), user.getId(), bo.getModel(), bo.getType()));
                //新增用户日志信息
                logMapper.insert(new UserLog(user.getId(),"用户注册",SystemConfig.USER_TYPE_ADD));
                //通过MQ 实现新人有礼
                template.convertAndSend("", RabbitMQConfig.Q_NEW,user.getId());
                //注册成功即登陆 生成令牌

                return R.ok();
            }else{
                return R.fail("亲，账号已存在！");
            }
        }
        return R.fail("亲，参数异常！");
    }

    @Override
    public R login(UserAddBo bo) {
        return null;
    }

    @Override
    public R findPass(UserFindBo bo) {
        return null;
    }

    @Override
    public R checkToken(String token) {
        return null;
    }

    @Override
    public R queryByToken(String token) {
        return null;
    }

    @Override
    public R loginout(String token) {
        return null;
    }
}
